package com.fmi.timedealer.achievement;

import java.io.Serializable;
import java.util.ArrayList;

public class Achievement implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5279295539409629487L;
	private ArrayList<AchievementLevel> levels;
	private String name;
	private String goalMessage;
	private int id;
	private boolean isCompleted;
	
	public boolean isCompleted() {
		return isCompleted;
	}

	public void setCompleted(boolean isCompleted) {
		this.isCompleted = isCompleted;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Achievement(int id, ArrayList<AchievementLevel> lvls, String name, String message, boolean isCompleted) {
		this.id = id;
		this.levels = lvls;
		this.name = name;
		this.goalMessage = message;
		this.isCompleted = isCompleted;
	}
	
	public Achievement(int id, String name, String message, boolean isCompleted) {
		this(id, new ArrayList<AchievementLevel>(), name, message, isCompleted);
	}
	
	public Achievement() {
		this(0, new ArrayList<AchievementLevel>(), "", "", false);
	}
	
	public void addLevel(AchievementLevel level) {
		if (this.levels == null) {
			this.levels = new ArrayList<AchievementLevel>();
		}
		this.levels.add(level);
	}
	
	public int getMaxLevel() {
		int max = 0;
		for (AchievementLevel level : levels) {
			if (max < level.getLevel()) {
				max = level.getLevel();
			}
		}
		return max;
	}
	
	public void removeLevel(AchievementLevel level) {
		if (this.levels != null) {
			this.levels.remove(level);
		}
	}
	
	public ArrayList<AchievementLevel> getLevels() {
		return levels;
	}
	public void setLevels(ArrayList<AchievementLevel> levels) {
		this.levels = levels;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGoalMessage() {
		return goalMessage;
	}
	public void setGoalMessage(String goalMessage) {
		this.goalMessage = goalMessage;
	}
	
	public int getCompletedLevels() {
		return levels.size()/2;
	}
	
	public void updateCompleted() {
		boolean completed = true;
		for (AchievementLevel level : this.levels) {
			if (!level.isCompleted()) {
				completed = false;
				break;
			}
		}
		this.isCompleted = completed;
	}
}
